package com.btkjgc.controller;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AdminUser;
import com.btkjgc.models.extend.IndexModel;
import com.btkjgc.utils.HibernateUtil;

@Controller
public class WelcomeController
{
	@RequestMapping(value = { "/" }, method = { RequestMethod.GET })
	public ModelAndView getFirstPage(HttpServletRequest request)
	{
		return new ModelAndView("redirect:/login");
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = { "index" }, method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView index(HttpServletRequest request)
	{
		ModelAndView mv;
		AdminUser adminUser = (AdminUser) request.getSession().getAttribute("user");
 
		mv = new ModelAndView("index");
		AdminUser au = (AdminUser) request.getSession().getAttribute("user");
		mv.addObject("username", au.getUsername());
 
 
		String sql="select \n" +
				"0 as nowDayrecord,\n" +
				"(select count(*) from exception_record_t ert where date(ert.createdate) = curdate()  and ert.type=0 and ert.del=0)  personRefund,\n" +
				"(select count(*) from exception_record_t ert where date(ert.createdate) = curdate()  and ert.type=1 and ert.del=0)  companyRefund,\n" +
				"(select count(*) from return_money_t rmt where rmt.begintime<=now() and rmt.endtime>=now() and rmt.del=0) returnMoney,\n" +
				"(select count(*) from coupon_t ct where   (ifnull(ct.begintime,'0000-1-1')<=now() and ifnull(ct.endtime,'9999-12-31')>=now() and ct.status=1 and ct.del=0))  couponNum\n" +
				" from dual";
		
		IndexModel indexModel=(IndexModel) HibernateUtil.executeSQLQueryObject(sql, IndexModel.class);
		mv.addObject("indexModel", indexModel);
		
		
		return mv;
	}
	
	@RequestMapping(value = "get_nowtime")
	@ResponseBody
	public IndexModel getNowtime(){
		
		String d=new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		
		IndexModel indexModel = new IndexModel();
		indexModel.setNowtime(d);
		
		return indexModel;
	}
}
